<?php
namespace Shoptaepc\Controller;
use Think\Controller;
// 本代码运行在EWS端
class JsonController extends Controller {
	public function index(){
		$this->sid = (int)I("sid");
		$EV = D("Events"); //活动列表，同时只有1个活动
		$EV->sid=$this->sid;
		$EV->getCurEvent();
		if(!$EV->hasEvent){
			$this->ajaxReturn(Array("hasEvent"=>"false"),"JSON");
			die();
		}
		$cfg = $EV->cfg;
		$countdown = $EV->countdown;
		$gifts = $EV->gifts;

		
		if($cfg["curtool"]=="dices"||$cfg["curtool"]=="wheels"){
			$TS = D("ToolsStatus");
			$TS->curtool=$cfg["curtool"];
			$dicescfg=$TS->dicescfg;
		}
		
		$G = D("Shopadmin/UsersGifts"); //前端显示滚动的中奖名单
		$G->sid=$this->sid;
		$G->where="sm_events_gifts.type<>7 AND  `sm_events_gifts`.`event_id`=".$cfg["id"];
		$G->limit="10";
		$G->isfront=true;
		$winner = $G->SendList();
		foreach($winner as $k=>$v){
			$winner[$k]["usernick"]= urlencode(iconv("UTF-8","gbk//TRANSLIT",(msubstr($winner[$k]["usernick"],0,2,"UTF-8","*",3))));
			$winner[$k]["name"]=urlencode(iconv("UTF-8","gbk//TRANSLIT",$winner[$k]["name"]));
		}
		foreach($gifts as $k=>$v){
			$gifts[$k]["name"]=urlencode(iconv("UTF-8","gbk//TRANSLIT",$gifts[$k]["name"]));
			$gifts[$k]["type"]=urlencode(iconv("UTF-8","gbk//TRANSLIT",$gifts[$k]["type"]));
			$gifts[$k]["level_name"]=urlencode(iconv("UTF-8","gbk//TRANSLIT",$gifts[$k]["level_name"]));
		}
		$cfgok=true;
		$front_cfg = json_decode(htmlspecialchars_decode($cfg["front_cfg"]),true);
		unset($cfg["front_cfg"]);
		
		$data["dicescfg"]=$dicescfg;
		$data["front_cfg"]=$front_cfg;
		$data["ddcj"]=$cfg["orderset"];
		$data["errmsg"]=$errmsg;
		$data["cfgok"]=$cfgok;
		$data["contdown"]=$countdown;
		$data["cfg"]=$cfg;
		$data["ctp"]=$cfg["curtool"];
		// $data["ctptpl"]="./".$cfg["curtool"].".inc.html";
		$data["lotts"]=$cfg["lotts"];
		$data["wins"]=$cfg["wins"];
		$data["gifts"]=$gifts;
		$data["winner"]=array_values($winner);
		$this->ajaxReturn($data,"JSON");
	}
	
	public function test(){
		$this->suid=1111;
		$SVIEW = D("Shopmodule/UserseedsView");
		$uwh=$this->usernick?" AND usernick='".$this->usernick."'":"";
		$vwh=$this->suid?" AND suid='".$this->suid:"";
		$days = $SVIEW->where("seeds.sid=".$this->sid.$uwh.$vwh." AND seeds.addtime>='$firstday' AND seeds.addtime<='$lastday' AND (seeds.reason=".C("SEEDS_REASON")["qiandao"]["id"]." OR seeds.reason=".C("SEEDS_REASON")["buqian"]["id"].")")->getField("DATE_FORMAT(seeds.addtime,'%Y-%m-%d') AS addtime,type,reason");
		echo $SVIEW->_sql();
	}
}